﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Job.Web.Framework;
using Job.Entity;
using Job.Logic.Company;

namespace Job.Web.userControls.Company
{
    public partial class PositionInfo : BaseCtrl
    {
        PositionLogic logic = new PositionLogic();
        private int CompanyId
        {
            get
            {
                //后台管理员能通过后台为企业增加职位，所以在此处需要兼职企业ID从url或从Session中获取
                int id = myR["company"].IntValue;
                if (id > 0) return id;
                MySession session = new MySession();
                SessionCompany sCompany = session.GetCompanyFromSession();
                if (null == sCompany)
                    return 0;
                else
                    return sCompany.CompanyId;
            }
        }

        private int PositionId
        {
            get { return myR["position"].IntValue; }
        }




        protected void Page_Load(object sender, EventArgs e)
        {
            if (0 == CompanyId)
            {
                Response.Redirect("CompanyLogin.aspx?categoryid=2&error=1");
                return;
            }

            if (!IsPostBack)
            {
                LoadBaseInfo();
            }
        }



        private bool LoadBaseInfo()
        {
            JobPositionInfo info = null;
            if (this.PositionId > 0)
            {
                info = logic.GetPosition(PositionId);
                if (null == info)
                {
                    ShowMessage("指定的职位已不存在！");
                    btnSave.Enabled = false;
                    return false;
                }
                else
                {
                    if (info.CompanyInfoId != CompanyId)
                    {
                        ShowMessage("您无权限管理指定的职位！");
                        btnSave.Enabled = false;
                        return false;
                    }
                }
            }
            else
            {
                info = new JobPositionInfo(); 
            }

            tbName.Text = info.PositionName;
            WebCommon.LoadLevelDropDownList(ddlClassFather, ddlClassSub, ClassRelate,
                Constants.GetParentCodeBySubCode(info.PositionClass),
                info.PositionClass, Constants.DictionaryType.POSITION_CLASS);

            string education = string.Empty;
            if (info.EduLimit.HasValue) education = info.EduLimit.ToString();
            WebCommon.SetDropDownFromDict(ddlEduction, education,
                Constants.GetDictionaryCode(Constants.DictionaryType.EDUCATION_LEVEL),true);

            tbNumber.Text = info.PersonLimit.ToString();
            string sexy = "3";
            if (info.SexyLimit.HasValue) sexy = info.SexyLimit.ToString();
            this.rblSexy.Text = sexy;
            tbHeight.Text = info.HeightLimit.ToString();

            string strAge;
            ListItem item1 = new ListItem("不限","0");
            ddlAgeMax.Items.Add(item1);
            ListItem item2 = new ListItem("不限","0");
            ddlAgeMin.Items.Add(item2);
            
            for( int age = 18 ; age <= 60 ; age++ )
            {
                strAge = age.ToString();
                ddlAgeMin.Items.Add( strAge );
                ddlAgeMax.Items.Add( strAge );
            }
            if ( info.AgeLower.HasValue ) ddlAgeMin.Text = info.AgeLower.ToString();
            if ( info.AgeHigh.HasValue ) ddlAgeMax.Text = info.AgeHigh.ToString();

            ListItem item = new ListItem("不限","0");
            ddlWorkLen.Items.Add(item);
            for( int len = 0 ; len <= 40; len++ )
            {
                ddlWorkLen.Items.Add( len.ToString());
            }
            if ( info.WorkLen.HasValue ) ddlWorkLen.Text = info.WorkLen.ToString();

            string salary = string.Empty;
            if (info.SalaryLevel.HasValue) salary = info.SalaryLevel.ToString();
            WebCommon.SetDropDownFromDict(ddlSalary, salary,
                Constants.GetDictionaryCode(Constants.DictionaryType.SALARY_LEVEL),true);

            string lang = string.Empty;
            if (info.LangClass.HasValue) lang = info.LangClass.ToString();
            WebCommon.SetDropDownFromDict(ddlLang, lang,
                Constants.GetDictionaryCode(Constants.DictionaryType.FOREIGN_LANG), true);
            string skill1 = string.Empty;
            if (info.LangSkill.HasValue) skill1 = info.LangSkill.ToString();
            WebCommon.SetDropDownFromDict(ddlLangSkill, skill1,
                Constants.GetDictionaryCode(Constants.DictionaryType.LANG_SKILL), true);
                
            string certify = string.Empty;
            if (info.LangCertify.HasValue) certify = info.LangCertify.ToString();
            WebCommon.SetDropDownFromDict(ddlLangCertify, certify,
                Constants.GetDictionaryCode(Constants.DictionaryType.LANG_CERTIFY), true);

            string eatRoom = string.Empty;
            if(info.EatRoom.HasValue) eatRoom = info.EatRoom.ToString();
            WebCommon.SetDropDownFromDict(ddlEatRoom, eatRoom,
                Constants.GetDictionaryCode(Constants.DictionaryType.EAT_ROOM), true);

            string skill2 = string.Empty;
            if (info.DialectSkill.HasValue) skill2 = info.DialectSkill.ToString();
            WebCommon.SetDropDownFromDict(ddlDialect, skill2,
                Constants.GetDictionaryCode(Constants.DictionaryType.LANG_SKILL), true);

            if ( info.EndDate != null && info.EndDate.HasValue && info.EndDate.Value != null )
                tbEndDate.Text = info.EndDate.Value.ToString("yyyy-MM-dd");
            tbEndDate.Attributes.Add("onfocus", "calendar()");

            WebCommon.LoadLevelDropDownList(ddlAddrFather, ddlAddrSub, AddressRate,
                Constants.GetParentCodeBySubCode(info.WorkAddr),
                info.WorkAddr, Constants.DictionaryType.ADDR_CITY);

            string type = "0";
            if (info.RecruitStyle.HasValue) type = info.RecruitStyle.ToString();
            rblType.Text = type;

            tbMail.Text = info.ContactEmail;
            tbDesc.Text = info.PositionDesc;

            return true;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                JobPositionInfo info = new JobPositionInfo();
                info.PositionInfoId = PositionId;
                info.PositionName = tbName.Text.Trim();
                info.PositionClass = ddlClassSub.Text;
                if (ddlEduction.SelectedValue.Length > 0)
                    info.EduLimit = Convert.ToInt32(ddlEduction.SelectedValue);
                if (tbNumber.Text.Trim().Length > 0)
                    info.PersonLimit = Convert.ToInt32(tbNumber.Text.Trim());
                info.SexyLimit = Convert.ToInt32(rblSexy.SelectedValue);
                if (tbHeight.Text.Trim().Length > 0)
                    info.HeightLimit = Convert.ToInt32(tbHeight.Text.Trim());
                if (ddlAgeMin.SelectedValue.Length > 0)
                    info.AgeLower = Convert.ToInt32(ddlAgeMin.SelectedValue);
                if (ddlAgeMax.SelectedValue.Length > 0)
                    info.AgeHigh = Convert.ToInt32(ddlAgeMax.SelectedValue);
                if (ddlWorkLen.SelectedValue.Length > 0)
                    info.WorkLen = Convert.ToInt32(ddlWorkLen.SelectedValue);
                if (ddlSalary.SelectedValue.Length > 0)
                    info.SalaryLevel = Convert.ToInt32(ddlSalary.SelectedValue);
                if (ddlLang.SelectedValue.Length > 0)
                    info.LangClass = Convert.ToInt32(ddlLang.SelectedValue);
                if (ddlLangSkill.SelectedValue.Length > 0)
                    info.LangSkill = Convert.ToInt32(ddlLangSkill.SelectedValue);
                if (ddlLangCertify.SelectedValue.Length > 0)
                    info.LangCertify = Convert.ToInt32(ddlLangCertify.SelectedValue);
                if (ddlEatRoom.SelectedValue.Length > 0)
                    info.EatRoom = Convert.ToInt32(ddlEatRoom.SelectedValue);
                if (ddlDialect.SelectedValue.Length > 0)
                    info.DialectSkill = Convert.ToInt32(ddlDialect.SelectedValue);
                if (tbEndDate.Text.Trim().Length > 0)
                    info.EndDate = Functions.GetDateTime(tbEndDate.Text.Trim());
                info.WorkAddr = ddlAddrSub.Text;
                info.RecruitStyle = Convert.ToInt32(rblType.Text);
                info.ContactEmail = tbMail.Text;
                info.PositionDesc = tbDesc.Text;

                info.CompanyInfoId = CompanyId;

                if (info.PositionInfoId <= 0)
                {
                    info.CurStatus = true;
                    logic.Insert(info);
                }
                else
                {
                    logic.Update(info);
                }
                ShowMessage("保存职位信息成功！");
            }
            catch (Exception ex)
            {
                ShowMessage("保存失败：<br>" + ex.Message);
            }

        }


        private void ShowMessage(string message)
        {
            this.message.InnerHtml = "<font color=red>" + message + "</font>";
            this.message.Visible = true;
            this.message.Height = "30px";
            this.message.VAlign = "middle";
        }



    }
}